package com.dt.common.validation.impl;

import java.util.HashMap;
import java.util.Map;

import com.dt.common.validation.itf.AbandonedDefinition;
import com.dt.common.validation.itf.PropertyGetter;
import com.dt.common.validation.itf.ValidatioinBuilder;
import com.dt.common.validation.itf.ValidationDefinition;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@EqualsAndHashCode(of = { "propertyName", "name" })
@ToString(of = { "propertyName", "name" })
public abstract class DefaultValidationDefinition
		implements ValidationDefinition, ValidatioinBuilder, AbandonedDefinition {

	/**
	 * validation名称
	 */
	protected String name;

	/**
	 * 被检查属性名称
	 */
	protected String propertyName;

	/**
	 * 错误提示信息
	 */
	protected String rawMsg;

	/**
	 * 是否该检查需要废弃去除
	 */
	protected boolean isAbandoned = false;

	/**
	 * 获取待检查属性值
	 */
	protected PropertyGetter propertyGetter;

	@Override
	public String getResolvedMsg() {
		String msg = new String(rawMsg);

		Map<String, String> params = new HashMap<>();
		fillMsgInterpolatingParams(params);
		if (params != null) {
			for (Map.Entry<String, String> param : params.entrySet()) {
				String pattern = "\\$\\{" + param.getKey() + "\\}";
				msg = msg.replaceAll(pattern, param.getValue());
			}
		}
		return msg;
	}

	protected void fillMsgInterpolatingParams(Map<String, String> params) {
	}

}
